$(function(){
	/**调用初始化方法*/
	/*getAllRole();*/
	queryRole();
});
function getAllRole(){
	$.post("/UserGetAllRoleWorker/",null,function(str){
		str = decodeURI(str);
		var data = $.parseJSON(str);
		for(var i=0;i<data.length;i++){
			$("#selectId").append("<option value='"+data[i].RoleId+"'>"+data[i].RoleName+"</option>");
		}
	});
}
/*function queryForm(){
	var userName = $("#queryName").val();
	var roleId = $("#querySelect").val();
	queryRole(userName,roleId);
}*/
function queryRole(){
	$(this).hide();
	$('#usertable').bootstrapTable('destroy');
	$('#usertable').bootstrapTable({
		method: 'get',
		url: '/GetHistoryAccount/',
		cache: false,
		striped: true,
		pagination:true,
		pageSize:20,
		pageList: [5, 10, 20,50],
		pageNumber:1,
		sidePagination:'server',
		height:750,
		width:1600,
		paginationPreText: "上一页",
		paginationNextText: "下一页",     
		paginationHAlign: "left",
		paginationDetailHAlign: "right",
		columns: [
			{
				field: 'insertTime',
				title: '插入时间',
				align: 'center',
				formatter: turnAround,
				valign: 'middle'
			},{
				field: 'gameName',
				title: '游戏',
				align: 'center',
				formatter: turnAround,
				valign: 'middle'
			},{
				field: 'room',
				title: '房间',
				align: 'center',
				formatter: turnAround,
				valign: 'middle'
			},
			{
				field: 'table',
				title: '桌子',
				align: 'center',
				formatter: turnAround,
				valign: 'middle'
			}, {
				field: 'userNum',
				title: '用户数量',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'
			},{
				field: 'robotNum',
				title: '机器人数量',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'
			},{
				field: 'loss',
				title: '消耗',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'
			},{
				field: 'rat',
				title: '税收',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'
			},{
				field: 'coin',
				title: '金币',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'
			},{
				field: 'startTime',
				title: '开始时间',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'
			},{
				field: 'endTime',
				title: '结束时间',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'
			}]
	});
}
/**
 * table图标区
 */
function operateFormatter(value, row, index) {
	return [
		'<button class="updateUser" id="updateUser">修改</button>'
		].join('');
}

/**
 * 冻结管理员
 * @returns
 */
$(".clockUser").click(function(){
	var userids= getUserTableCheckBoxIds();
	$.post("/operationManegerClockUser/",{"userids":userids},function(data){
		var data1 = $.parseJSON(data);
		alert(data1.message);
    	if(data1.state == 1){
    		window.location.reload();
    	}
	});
});
/**
 * 解冻管理员
 * @returns
 */
$(".dclockUser").click(function(){
	var userids= getUserTableCheckBoxIds();
	$.post("/operationManegerDClockUser/",{"userids":userids},function(data){
		var data1 = $.parseJSON(data);
		alert(data1.message);
    	if(data1.state == 1){
    		window.location.reload();
    	}
	});
});
/**
 * 删除管理员
 * @returns
 */
$(".deleteUser").click(function(){
	var userids= getUserTableCheckBoxIds();
	$.post("/operationManegerDeleteUser/",{"userids":userids},function(data){
		var data1 = $.parseJSON(data);
		alert(data1.message);
    	if(data1.state == 1){
    		window.location.reload();
    	}
	});
});
function getUserTableCheckBoxIds(){
	var a=$('#usertable').bootstrapTable('getSelections');
	var ids="";
	if(a.length<1){
		alert("请至少选择一行操作");
	}else{
		var len=a.length;
		ids=a[0].uid;
		for(var i=1;i<len;i++){
			ids+=(","+a[i].uid);
		}
		return ids;
	}
}
/**
 * 删除角色
 * @returns
 */
$(".deleteRole").click(function(){
	var roleids=getRoleTableCheckBoxIds();
	alert(roleids);
	$.post("/deleteRole/",{"roleids":roleids},function(data){
		var data1 = $.parseJSON(data);
		alert(data1.message);
    	if(data1.state == 1){
    		window.location.reload();
    	}
	});
});
function getRoleTableCheckBoxIds(){
	var a=$('#Roletable').bootstrapTable('getSelections');
	var ids="";
	if(a.length<1){
		alert("请至少选择一行操作");
	}else{
		var len=a.length;
		ids=a[0].roleId;
		for(var i=1;i<len;i++){
			ids+=(","+a[i].roleId);
		}
		return ids;
	}
}
/**
 * 图标绑定点击事件
 */
window.operateEvents = {
		'click .updateUser': function (e, value, row, index) {
			isModify=1;
			$("#titleId").html("修改用户");
			$(".username").val(decodeURI(row.uname,"utf-8"));
			$(".password").val(decodeURI(row.upassword,"utf-8"));
			$("#selectId").val(row.roleid);
			$("#userIds").val(row.uid);
			$("#show_div_user").show();
		}
};

function turnAround(value, row, index){
	return decodeURI(value,"utf-8"); 
}



/* 点击提交的事件  */
$("#addUser").click(function(){
    $(".show_div").hide();
    var data={};
    var userName=$(".username").val();
    var chinaName=$(".password").val();
    var roleSId=$("#selectId").val();
    data.userName = userName;
    data.password = chinaName;
    data.roleSId = roleSId;
    var userid = $("#userIds").val();
    if(userid!=0 && userid!=null){
    	data.userid = userid;
    }
    if(userName && chinaName  && roleSId){
    	$(this).html('发送中...');
        $(this).css({"background":"#616161"});
        $.get("/UserAddAndUpdateWorker/",data,function(data){
        	data = decodeURI(data);
	    	var data1 = $.parseJSON(data);
	    	if(data1.state == 1){
	    		window.location.reload();
	    		alert(data1.message);
	    	}else{
	    		$(".showList_submit").html('提交');
	    		$(".showList_submit").css({"background":"#438EB9"});
	    		alert(data1.message);
	    		$("#show_div_user").show();
	    	}
        });
    }else{
        alert("填写错误");
    }
});
